package com.acane.instagram.feature.friend

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.acane.instagram.core.model.UserInfo
import com.acane.instagram.core.model.network.datasource.MyRetrofitDatasource
import com.acane.instagram.feature.common.SessionManager
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

class FriendViewModel : ViewModel() {
    // 用于显示朋友消息列表
    private val _friendMessageList = MutableStateFlow<List<UserInfo>>(emptyList())

    // 用于显示最新的消息
    private val _latestMessage = MutableStateFlow<String?>(null)

    var friendMessageList: MutableStateFlow<List<UserInfo>> = _friendMessageList

    var latestMessage: MutableStateFlow<String?> = _latestMessage


    suspend fun getFriendMessageList(): List<UserInfo>? {
        return MyRetrofitDatasource.getFriendMessageList(SessionManager.currentUser?.userName).data
    }

    init {
        loadData()
    }

     fun loadData() {
         viewModelScope.launch {
             Log.d("FriendViewModel", "loadingMessageData")
             val loadedMessages = getFriendMessageList()
             if (loadedMessages != null) {
                 friendMessageList.value = loadedMessages
             } else {
                 friendMessageList.value = emptyList()
             }
             Log.d("FriendViewModel", "dataLoaded:$friendMessageList")
         }
//         viewModelScope.launch {
//             latestMessage = MyRetrofitDatasource.getChatHistory(SessionManager.currentUser?.userName, )
//         }
    }
}